<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    dataInfo: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      chart: null,
      data: []
    }
  },
  watch: {
    dataInfo: {
      deep: true,
      handler(val) {
        this.coverData();
      },
    },
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.initChart()
    // })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    coverData() {
      if(!this.dataInfo) {
        this.data = []
      } else {
        this.data = [
          { value: this.dataInfo.procurementAmt, name: '采购金额' },
          { value: this.dataInfo.foreignAidAmt, name: '外援金额' },
          { value: this.dataInfo.travelAmt, name: '差旅金额' },
          { value: this.dataInfo.taskAmt, name: '工单金额' },
          { value: this.dataInfo.taxAmt, name: '税金金额' },
          { value: this.dataInfo.devAmt, name: '软件研发金额' },
          { value: this.dataInfo.firmAmt, name: '公司管理金额' },
          { value: this.dataInfo.otherAmt, name: '其他金额' },
        ]
      }
      this.$nextTick(() => {
        this.initChart()
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.chart.on("click", (params) => {
      //   console.log("点击的柱子数据:", params);
      //   alert(`类别: ${params.name}\n值: ${params.value}`);
      //   this.$emit("toIndex", params)
      // });
      this.chart.setOption({
        title: {
          text: '实际支出',
          left: 'left',
          textStyle: {
            fontSize: 14 // 移动端标题字体适当缩小
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b}: {c} ({d}%)' // 显示名称、数值和占比
        },
        legend: {
          type: 'scroll', // 图例支持滚动
          orient: 'horizontal',
          bottom: 0,
          textStyle: {
            fontSize: 10 // 减小图例字体
          }
        },
        series: [
          {
            name: '费用金额',
            type: 'pie',
            radius: ['40%', '70%'], // 改为环形图
            center: ['50%', '50%'], // 图表居中
            data: this.data,
            label: {
              fontSize: 10, // 数据标签字体大小
              formatter: '{b}\n{d}%' // 标签显示名称和占比
            },
            labelLine: {
              length: 10, // 标签连接线长度
              length2: 5
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    }
  }
}
</script>
